package com.svm.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.svm.operators.ImageResize;

public class Teste {

	public static void main(String[] args) throws IOException {

		BufferedImage img = ImageResize.resize(
				"D:\\imagens\\simplicity\\africanos\\7.jpg", 200, 200);

		System.out.println(img.getRGB(0, 0));

		Color c = new Color(img.getRGB(0, 0));

		System.out.println(c.getRed());

		Double[][] dct = transform(getFeatures(img), 200);

		for (int i = 0; i < 200; i++) {

			for (int j = 0; j < 200; j++) {

				if ((j > 25) && (i > 50)) {

					dct[i][j] = 0.;
				}
			}
		}

		Double[][] idct = inverse(dct, 200);

		BufferedImage newImage = new BufferedImage(200, 200,
				BufferedImage.TYPE_INT_RGB);

		for (int i = 0; i < 200; i++) {

			for (int j = 0; j < 200; j++) {

				Color color = new Color(Math.abs(idct[i][j].intValue()),
						Math.abs(idct[i][j].intValue()), Math.abs(idct[i][j]
								.intValue()));

				newImage.setRGB(j, i, color.getRGB());
			}
		}

		ImageIO.write(newImage, "JPEG", new File(
				"C:\\Users\\Douglas\\Desktop\\2.jpg"));

		System.out.println("done");

	}

	private static Double[][] transform(Double[][] img, int size) {

		Double[][] ret = new Double[size][size];
		Double tmp;

		for (int u = 0; u < size; u++) {

			for (int v = 0; v < size; v++) {

				ret[u][v] = 0.;

				for (int i = 0; i < size; i++) {

					for (int j = 0; j < size; j++) {

						tmp = img[i][j] * -1;
						tmp *= Math.cos((((2 * i) + 1) * u * Math.PI)
								/ (2 * size));
						tmp *= Math.cos((((2 * j) + 1) * v * Math.PI)
								/ (2 * size));

						ret[u][v] += tmp;
					}
				}

				tmp = 4.;
				tmp *= u == 0 ? 1 / Math.sqrt(2) : 1;
				tmp *= v == 0 ? 1 / Math.sqrt(2) : 1;
				tmp /= Math.pow(size, 2);

				ret[u][v] *= tmp;

			}

		}

		return ret;
	}

	private static Double[][] inverse(Double[][] img, int size) {

		Double[][] ret = new Double[size][size];
		Double tmp;

		for (int i = 0; i < size; i++) {

			for (int j = 0; j < size; j++) {

				ret[i][j] = 0.;

				for (int u = 0; u < size; u++) {

					for (int v = 0; v < size; v++) {

						tmp = u == 0 ? 1 / Math.sqrt(2) : 1;
						tmp *= v == 0 ? 1 / Math.sqrt(2) : 1;
						tmp *= img[u][v];
						tmp *= Math.cos((((2 * i) + 1) * u * Math.PI)
								/ (2 * size));
						tmp *= Math.cos((((2 * j) + 1) * v * Math.PI)
								/ (2 * size));

						ret[i][j] += tmp;

					}
				}

				ret[i][j] *= -1;

			}
		}

		return ret;
	}

	private static Double[][] getFeatures(BufferedImage image) {

		Color color;
		Double[][] vector = new Double[200][200];

		for (int y = 0; y < image.getHeight(); y++) {

			for (int x = 0; x < image.getWidth(); x++) {

				color = new Color(image.getRGB(x, y));
				vector[y][x] = Double.parseDouble("" + color.getRed() + "");

			}
		}

		return vector;

	}

}
